import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { getHomeHotDistinationData, getHomeGoodPriceData, getHomeHighScoreData, getHomeExploreData, getHomeYearnCityData, getHomePlusData } from "@/services/modules/home";

export const fetchHomeDataAction = createAsyncThunk("getHomeData", async (payload, { dispatch }) => {
  getHomeGoodPriceData().then(res => {
    dispatch(changeGoodPriceAction(res))
  })

  getHomeHighScoreData().then(res => {
    dispatch(changeHighScoreAction(res))
  })

  getHomeHotDistinationData().then(res => {
    dispatch(changeHotDistinationAction(res))
  })

  getHomeExploreData().then(res => {
    dispatch(changeExploreAction(res))
  })

  getHomeYearnCityData().then(res => {
    dispatch(changeYearnCityAction(res))
  })

  getHomePlusData().then(res => {
    dispatch(changePlusAction(res))
  })
})

const homeSlice = createSlice({
  name: 'home',
  initialState: {
    goodPriceInfo: null,
    highScoreInfo: null,
    hotDestinationInfo: null,
    exploreInfo: null,
    yearnCityInfo: null,
    plusInfo: null
  },
  reducers: {
    changeGoodPriceAction(state, { payload }) {
      state.goodPriceInfo = payload
    },
    changeHighScoreAction(state, { payload }) {
      state.highScoreInfo = payload
    },
    changeHotDistinationAction(state, { payload }) {
      state.hotDestinationInfo = payload
    },
    changeExploreAction(state, { payload }) {
      state.exploreInfo = payload
    },
    changeYearnCityAction(state, { payload }) {
      state.yearnCityInfo = payload
    },
    changePlusAction(state, { payload }) {
      state.plusInfo = payload
    }
  } 
})
export const { 
  changeGoodPriceAction,
  changeHighScoreAction,
  changeHotDistinationAction,
  changeExploreAction,
  changeYearnCityAction,
  changePlusAction
} = homeSlice.actions

export default homeSlice.reducer